package hjt.com.module_test.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import hjt.com.module_test.R;

public class CustomView extends View {

    private int defaultValue = 100;
    private int defaultColor = Color.GREEN;

    private Paint mPint;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
        defaultValue = array.getDimensionPixelSize(R.styleable.CustomView_defaultValue, defaultValue);
        defaultColor = array.getColor(R.styleable.CustomView_defaultColor, defaultColor);

        array.recycle();

        initPaint();

    }

    private void initPaint() {

        mPint = new Paint();
        mPint.setColor(defaultColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getSize(widthMeasureSpec);
        int height = getSize(heightMeasureSpec);

        if (width > height) {
            width = height;
        } else {
            height = width;
        }

        setMeasuredDimension(width, height);

    }

    private int getSize(int sizeMeasureSpec) {
        int mysize = defaultValue;
        int size = MeasureSpec.getSize(sizeMeasureSpec);
        int mode = MeasureSpec.getMode(sizeMeasureSpec);
        Log.e("sss_size", size + "");
        switch (mode) {
            case MeasureSpec.UNSPECIFIED:
                mysize = size;
                break;
            case MeasureSpec.AT_MOST:
                mysize = defaultValue;
                break;
            case MeasureSpec.EXACTLY:
                mysize = size;
                break;
        }
        return mysize;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int radiu = getMeasuredWidth() / 2;
        int x = radiu;
        int y = radiu;

        canvas.drawCircle(x, y, radiu, mPint);

    }

}
